package com.by.controller;

import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.pojo.dto.ProductPageQueryDTO;
import com.by.pojo.entity.Product;
import com.by.service.ProductManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "产品管理")
@RestController
@RequestMapping("/api")
public class ProductManageController extends BaseController {

    @Autowired
    private ProductManageService productService;

    @ApiOperation("保存产品")
    @PostMapping("/product")
    public ResultBean save(Product product) {
        int ans = productService.addProduct(product);
        return success("产品保存成功", ans);
    }

    @ApiOperation("查询产品列表")
    @PostMapping("/product/list")
    public ResultBean<PageBean<List<Product>>> query(@RequestBody ProductPageQueryDTO productPageQueryDTO) {
        System.out.println("产品查询条件" + productPageQueryDTO);
        PageBean bean = productService.query(productPageQueryDTO);
        return success("产品列表查询成功", bean);
    }

    @ApiOperation("删除产品")
    @DeleteMapping("/product")
    public ResultBean deleteProduct(@RequestParam(required = true) Integer id) {
        int ans = productService.deleteProduct(id);
        return success("删除成功", ans);
    }

    @ApiOperation("修改产品")
    @PutMapping("/product")
    public ResultBean updateProduct(Product product) {
        int ans = productService.updateProduct(product);
        return updateResult(ans);
    }
}
